package dasregistry;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.biojava.services.das.dao.DasSourceManager;
import org.biojava.services.das.registry.DasRegistrySql;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class OrganismBean {
	private  String DATASOURCE_NAME ;
	
	
String scientificName;
String commonName;
String taxId;
String message;

public OrganismBean(){
	DATASOURCE_NAME = DasRegistrySql.DATASOURCE_NAME;
	scientificName="";
	commonName="";
	taxId="";
	message="";
}

/** put this coordinate System into the database 

 * return 1 if creation o.k
 * returns 0 is problem occured
 * @return flags
 */
public int create(){
	Connection conn = null;
	message = "" ;
	DasSourceManager dao = new DasSourceManager();
	if(!isParsableToInt(taxId)){
		message="Taxanomic Id must be a number!!!!";
		return 0;
	}

	DataSource dataSource = null;
	try {

		Context env = (Context) new InitialContext().lookup("java:comp/env");
		dataSource = (DataSource) env.lookup(DATASOURCE_NAME);

	} catch ( Exception e) {
		e.printStackTrace();
		return 0;
	}
	
	String[]name={scientificName};
	Integer[] taxidsKnown;
	
	try {
		conn = dataSource.getConnection() ;
		taxidsKnown = dao.getTaxIds(conn,name);
		if(taxidsKnown.length>0){
			message+="Scientific Name already exists in database!!!";
			return 0;
		}else{
			dao.createOrganism(conn, commonName, scientificName, Integer.parseInt(taxId));
		}
	} catch (SQLException e) {
		if ( conn != null ) {
			try {
				conn.close();
			} catch ( SQLException ex) {
				ex.printStackTrace();
			}
		// TODO Auto-generated catch block
		e.printStackTrace();
		return 0;
	}

		return 0;
	} 
	return 1;
}


private void createOrganism(Connection conn) {
	BeanFactory ctx=(new ClassPathXmlApplicationContext("springDAO.xml"));
	DasSourceManager dao=(DasSourceManager) ctx.getBean("dasSourceManager");
	
	//check to see if organism name exists already before updating
		String[]name={scientificName};
		Integer[] taxidsKnown;
		try {
			taxidsKnown = dao.getTaxIds(conn,name);
			if(taxidsKnown.length>0){
				message+="Scientific Name already exists in database!!!";
			}else{
				dao.createOrganism(conn, commonName, scientificName, Integer.parseInt(taxId));
				dao.pingLastModified(conn);
			}
		} catch (SQLException e) {
			if ( conn != null ) {
				try {
					conn.close();
				} catch ( SQLException ex) {
					ex.printStackTrace();
				}
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}
	    
	
	
	
}

public String getScientificName() {
	return scientificName;
}

public void setScientificName(String scientificName) {
	this.scientificName = scientificName;
}

public String getCommonName() {
	return commonName;
}

public void setCommonName(String commonName) {
	this.commonName = commonName;
}

public String getTaxId() {
	return taxId;
}

public void setTaxId(String taxId) {
	this.taxId = taxId;
}

public String getMessage(){ return message ; }




public boolean isParsableToInt(String i)
{
try
{
Integer.parseInt(i);
return true;
}
catch(NumberFormatException nfe)
{
return false;
}
}














}

